package org.transfer.web;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.transfer.model.RegisterTable;
import org.transfer.model.ResultBean;
import org.transfer.service.RegisterTableService;
import org.transfer.utils.database.JDBCMYUtils;

/**
 * 请求处理器
 * 
 * @author kuan
 *
 */
@RestController
public class HandleFileController {

	@Autowired
	RegisterTableService registerTableService;

	/**
	 * 主页测试用
	 * 
	 * @return
	 */
	@RequestMapping("")
	public String hello() {
		System.out.println("hello!");
		return "hello!";
	}

	/**
	 * 执行解密操作
	 * 
	 * @return
	 */
	@RequestMapping("/decryptFile")
	public ResultBean decryptFile() {
		try {
			// decryptFileService.decryptFile();
			return new ResultBean("true", "200", "");
		} catch (Exception e) {
			return new ResultBean("true", "500", e.toString());
		}
	}

	/**
	 * 执行解压操作
	 * 
	 * @return
	 */
	@RequestMapping("/decompression")
	public ResultBean decompression() {
		try {
			// decompressionService.decompression();
			return new ResultBean("true", "200", "");
		} catch (Exception e) {
			return new ResultBean("true", "500", e.toString());
		}
	}

	@RequestMapping("getTable")
	public String getTable() {
		RegisterTable registerTable = new RegisterTable();
		registerTable.setAreaCode("411503");
		registerTable.setCompanyCode("307000000");
		registerTable.setCompanyName("信阳市平桥区行政审批中心");
		return registerTableService.findFinanceName(registerTable);
	}

	@RequestMapping("getTableTest")
	public String getTableTest() {
		RegisterTable registerTable1 = new RegisterTable();
		registerTable1.setCompanyName("固始县人大常委会办公室");
		registerTable1.setCompanyCode("040000000");
		registerTable1.setAreaCode("411525");

		int i = registerTableService.deleteFinanceTables(registerTable1);
		RegisterTable registerTable = new RegisterTable();
		registerTable.setAreaCode("411503");
		registerTable.setCompanyCode("307000000");
		registerTable.setCompanyName("信阳市平桥区行政审批中心");
		RegisterTable entity = registerTableService.getTable(registerTable);
		System.out.println(entity.getCompanyName());
		return "true";
	}

	@RequestMapping("insertAccountBook")
	public String insertAccountBook() {
		try {
			Connection connection = JDBCMYUtils.getConnectionMs("");
			String sql = "select \n" + " distinct\n" + "       null as 乡镇代码\n" + "      ,'${b_dwdm}' as 单位代码\n"
					+ "      ,convert(varchar(64),LEFT(c.kjqj,4)) as 会计年度\n"
					+ "      ,'${b_industryid}'+convert(varchar(64),LEFT(c.kjqj,4))+'${b_dwdm}'+rtrim(b.ztbh)\n"
					+ "             as 会计电子账簿编号\n" + "      ,RTRIM(b.ztmc) as 会计电子账簿名称\n"
					+ "      ,'${b_dwmc}' as 会计核算单位\n" + "      ,'${b_industryname}' as 行业分类\n"
					+ "      ,null as AO单位代码\n" + "      ,null as 源数据包名称\n" + "      ,RTRIM(b.ztmc) as 电子账簿简称\n"
					+ "      ,null as AB账标志位\n" + "      ,null as 是否含调整分录\n" + "      ,1 as 预留字段1\n"
					+ "      ,null as 预留字段2 \n" + "      ,'${b_xzqh}' as 行政区划代码\n"
					+ "from r95m2_data.dbo.GL_Kmxx a,r95m2_data.dbo.gl_ztcs b,r95m2_data.dbo.gl_pznr c\n"
					+ "where convert(varchar(64),LEFT(c.kjqj,4))<>0 and b.ztbh not like '%99999%'";
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			List<Map<String, Object>> maps = JDBCMYUtils.execQuery(preparedStatement, new Object[0]);

			JDBCMYUtils.closeConnection(connection);

			int i = registerTableService.insertAccountBook(maps);
			System.out.println(i);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "true";
	}

}
